package its_models.expert_model.expert_help_util;

import database_handler.DatabaseManager;
import help_util.DateTime;
import help_util.IndexConverter;
import its_models.expert_model.ExpertModelProperties;
import model_controller.ControlLayer;
import sudoku_properties.BasicSudokuParam;
import sudoku_properties.SudokuGameInfoParam;

/************************************************************
 * Class Step Matirx (20090929)
 * 1. store all the rules used to solve the puzzle
 ************************************************************/
public class SkillsMatrix {

    public SkillsMatrix(ControlLayer inputController){
         this.Controller = inputController;
         skillsMatrixStorage = new int[NUM_CELLS_IN_UNIT][NUM_CELLS_IN_UNIT][LOGICAL_RULES.length];
         for(int i=0; i<skillsMatrixStorage.length; i++){
             for(int j=0; j<skillsMatrixStorage[i].length; j++){
                 for(int k=0; k<skillsMatrixStorage[i][j].length; k++){
                     /* 0: rules is not used 1: rule is Used*/
                     skillsMatrixStorage[i][j][k] = RULE_RESET;
                 }
             }
         }
         dbManager = new DatabaseManager(inputController);
    }

    // 20091030 phrase2 start
    public void addSkills(int indexX, int indexY, String logicalRule){
        int ruleIndex = SudokuGameInfoParam.getLogicalRulesIndex(logicalRule);
        skillsMatrixStorage[indexX][indexY][ruleIndex] = RULE_SET;
    }
    
    public void recordSkillsMatrix(){
        for(int i=0; i<skillsMatrixStorage.length; i++){
            for(int j=0; j<skillsMatrixStorage[i].length; j++){
                int gridID = IndexConverter.getIndex(i, j);
                if ( !(skillsMatrixStorage[i][j][0]==0 &&  skillsMatrixStorage[i][j][1]==0 &&  skillsMatrixStorage[i][j][2]==0
                        &&  skillsMatrixStorage[i][j][3]==0 &&  skillsMatrixStorage[i][j][4]==0 &&  skillsMatrixStorage[i][j][5]==0) ){
                    if(!dbManager.recordSkillsMatrix(gridID, skillsMatrixStorage[i][j][0], skillsMatrixStorage[i][j][1],
                            skillsMatrixStorage[i][j][2], skillsMatrixStorage[i][j][3], skillsMatrixStorage[i][j][4],
                            skillsMatrixStorage[i][j][5])){
                        dbManager.recordError(this.getClass().getName().toString(), "Cannot add Skills Matrix with GridID = "+gridID, DateTime.getTimestamp());
                    }
                }
            }
         }
    }
    // 20091030 phrase2 end
    
    /* variables */
    private int[][][] skillsMatrixStorage;
    private ControlLayer Controller;
    private DatabaseManager dbManager;

    final static int RULE_SET = 1;
    final static int RULE_RESET = 0;
    /* constant variables */
    /* BasicSudokuParam */
    final static int NUM_CELLS_IN_UNIT = BasicSudokuParam.getNumCellsInUnit();
    /* ExpertModelProperties*/
    final static String RULE_SEPARATOR = ExpertModelProperties.getRuleSeparator();
    /* SudokuGameInfoParam */
    final static String[] LOGICAL_RULES = SudokuGameInfoParam.getLogicalRules();
}

